package com.whfc.ms.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        // 必须设置 SecurityManager
        factoryBean.setSecurityManager(securityManager);

        // 添加自己的过滤器
        Map<String, Filter> filterMap = new LinkedHashMap<>();
        filterMap.put("msuser", new MsUserFilter());
        factoryBean.setFilters(filterMap);

        // 访问的是后端url地址为 /login的接口
        factoryBean.setLoginUrl("/ms/api/login");

        // 拦截器,配置不会被拦截的链接 顺序判断
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/ms/api/login", "anon");
        filterChainDefinitionMap.put("/ms/api/logout", "anon");
        filterChainDefinitionMap.put("/ms/api/wxlogin", "anon");
        filterChainDefinitionMap.put("/ms/api/third-login", "anon");
        filterChainDefinitionMap.put("/ms/api/sgitg-login", "anon");
        filterChainDefinitionMap.put("/ms/api/ws/msg", "anon");
        filterChainDefinitionMap.put("/ms/api/sgitg/mach/list/all", "anon");
        filterChainDefinitionMap.put("/ms/api/sgitg/mach/statistics/all", "anon");
        filterChainDefinitionMap.put("/ms/api/sgitg/mach/daily/all", "anon");
        filterChainDefinitionMap.put("/ms/api/**/anon/**", "anon");
        filterChainDefinitionMap.put("/mp/api/workPlan/work/reviewpageList", "anon");
        filterChainDefinitionMap.put("/mp/api/workPlan/areaList", "anon");
        filterChainDefinitionMap.put("/ms/api/**", "msuser");
        factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return factoryBean;
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm.
        securityManager.setRealm(msUserRealm());

        return securityManager;
    }

    @Bean
    public MsUserRealm msUserRealm() {
        MsUserRealm myShiroRealm = new MsUserRealm();
        return myShiroRealm;
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 强制使用cglib，防止重复代理和可能引起代理出错的问题 // https://zhuanlan.zhihu.com/p/29161098
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

}
